package com.wang.service.impl;

import com.wang.dao.IAccountDao;
import com.wang.pojo.Account;
import com.wang.service.ITransferService;

/**
 * Created by wangq on 2021/7/11.
 */
public class TransferServiceImpl implements ITransferService {
//    private IAccountDao iAccountDao = new AccountDaoImpl();

//    private IAccountDao iAccountDao = (IAccountDao) BeanFactory.getBeanInstance("IAccountDao");

    private IAccountDao iAccountDao;

    public void setIAccountDao(IAccountDao iAccountDao) {
        this.iAccountDao = iAccountDao;
    }

    public void setIAccountDao() {

    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, double transferAmount) throws Exception {
        // 通过卡号查询账户
        Account fromAccount = iAccountDao.findAccountByCardNo(fromCardNo);
        Account toAccount = iAccountDao.findAccountByCardNo(toCardNo);

        fromAccount.decreaseMoney(transferAmount); // 减去转账金额
        toAccount.addMoney(transferAmount); // 加上转账金额

        //更新账户
        iAccountDao.updateAccountMoneyByCardNo(fromAccount);
        iAccountDao.updateAccountMoneyByCardNo(toAccount);
    }
}
